﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Visifire.Charts;
using Visifire.Commons;


namespace VisiFire_Testing1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string sequence1 = "MALW"; // row    WTRLRPLLALLALWPPPPARAFVNQHLCGSHLVEALYLVCGERGFFYTPKARREVEGPQVGALELAGGPGAGGLEGPPQKRGIVEQCCASVCSLYQLENYCN
        string sequence2 = "MALW";    //column   WMRLLPLLALLALWGPDPAAAFVNQHLCGSHLVEALYLVCGERGFFYTPKTRREAEDLQVGQVELGGGPGAGSLQPLALEGSLQKRGIVEQCCTSICSLYQLENYCN
        byte[,] ByteChart; //array of byte
        public int _requiredLength = 4; //it defines window size
        int lastAdded = 0;
       


        public MainWindow()
        {
            try     // this keyword try to run that following code
            {
                InitializeComponent();      //this will run the window
                ByteChart = Compare(sequence1, sequence2);      // this function makes aur logical dot plot
                CreateChart(ByteChart);     //this function makes real dotplot by using logical dotplot mapping
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString()); 
            }    
        }
        public byte[,] Compare(string seq1, string seq2)
        {            
            byte ON = 0xff;
            byte OFF = 0x00;
            byte[,] chart = new byte[seq1.Length, seq2.Length];

            for (int row = 0; row < seq1.Length; row++)
            {
                for (int col = 0; col < seq2.Length; col++)
                {
                    if (seq1[row].Equals(seq2[col]))
                    {
                        if (IsMatch(row, col))
                        {
                            chart[row, col] = ON; 
                        }                        
                    }
                    else
                    {
                        chart[row, col] = OFF;
                    }
                }
            }
            return chart;
        }

        private bool IsMatch(int s1, int s2)
        {
            int match_length = (int)_requiredLength;

            if (match_length <= 2)
            {
                return sequence1[s1] == sequence2[s2];
            }

            bool hasMatch = true;
            for (int add = -1 * match_length / 2; hasMatch && add <= match_length / 2; add++)
            {
                int t1 = s1 + add;
                int t2 = s2 + add;
                if (t1 < 0 || t2 < 0 || t1 >= sequence1.Length || t2 >= sequence2.Length)
                    continue;

                if (sequence1[t1] != sequence2[t2])
                    hasMatch = false;
            }

            return hasMatch;
        }

        public void CreateChart(byte [,] ByteChart)
        {
            byte ON = 0xff;          
            
            //Chart chart = new Chart();
            //Title title = new Title(); 
            //title.Text ="DotPlot";            
            //chart.Titles.Add(title);
            //Axis axis = new Axis();
            //axis.Interval = 0.5;           
         
            DataSeries dataSeries = new DataSeries();
            dataSeries.RenderAs = RenderAs.Point;
            dataSeries.Color = Brushes.DarkGray;
            dataSeries.LightWeight = false;
            for (int row = 0; row < ByteChart.GetLength(0); row++)
            {
                for (int col = 0; col < ByteChart.GetLength(1); col++)
                {
                    if (ByteChart[row,col].Equals(ON))
                    {
                        MyDataPoint myDataPoint = new MyDataPoint(row, col,ref sequence1);
                        //myDataPoint.MakeDotSequenceIndexes(_requiredLength);
                        //myDataPoint.MakeDotContent(sequence1);                       
                        dataSeries.DataPoints.Add(myDataPoint);                      
                    }                   
                }
            }
            chart.Series.Add(dataSeries);
            //MyGrid.Children.Add(chart);
        }

        public class MyDataPoint : DataPoint
        {
            //int _windowSize = 0;
            int StartIndex = 0;
            int EndIndex = 0;
            char _content;
            string Sequence = null;
            int _xVal = 0;
            //int _yVal = 0;


            //public int WindowSize
            //{
            //    get { return _windowSize; }
            //    set { _windowSize = value; }
            ////}

            public char Content
            {
                get { return _content; }
                set { _content = value; } 
            }

            public MyDataPoint()
            {
            }
            public MyDataPoint(int Xval, int YVal, ref string Refsequence)
            {
                Sequence = Refsequence;
                XValue = Xval;
                YValue = YVal;
                _xVal = Xval;
                //_yVal = YVal;
                this.MouseLeftButtonDown += new MouseButtonEventHandler(dp_MouseLeftButtonDown);
            }
            void dp_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)           
            {
                MakeDotContent(Sequence);
                MessageBox.Show(Content.ToString());
            }
            //public void MakeDotSequenceIndexes(int WindowSize)
            //{
            //    if (WindowSize > 2)
            //    {
            //        StartIndex = (_xVal) - (1 * WindowSize / 2);
            //        if (StartIndex < 0)
            //        {
            //            StartIndex = 0; 
            //        }
            //        EndIndex = _xVal + (WindowSize / 2);   
            //    }                              
            //}

            public void MakeDotContent(string seq)
            {
                //Content = Sequence.Substring(StartIndex, (EndIndex-StartIndex));
                Content = Sequence.ElementAt(_xVal);               
            }

        }

        public class MyLightDataPoint : LightDataPoint
        {
            public MyLightDataPoint()
            { }

            public MyLightDataPoint(int Xval, int YVal)
            {
                XValue = Xval;
                YValue = YVal;
            }
        }
        
        
    }      

   

}
